package com.guodi.map.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.core.secure.LoginUser;
import com.guodi.core.tool.utils.Func;
import com.guodi.map.entity.MapLayerCollect;
import com.guodi.map.entity.MapProjectDir;
import com.guodi.map.mapper.MapLayerCollectMapper;
import com.guodi.map.service.IMapLayerCollectService;
import com.guodi.map.service.IMapProjectDirService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MapLayerCollectServiceImpl extends ServiceImpl<MapLayerCollectMapper, MapLayerCollect>  implements IMapLayerCollectService {

    @Resource
    private MapLayerCollectMapper mapLayerCollectMapper;
    @Resource
    private IMapProjectDirService iMapProjectDirService;

    /**
    * @描述: 获取用户收藏的图层，专题图层必须存在，专题图层必须已经授权给这个用户
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2022/4/28 19:05
    **/
    @Override
    public List<MapProjectDir> listUserCollectLayers(LoginUser user, String projectId){
        List<String> roleIds = Func.toStrList( ",", user.getRoleId());
        if(roleIds == null || roleIds.isEmpty()){
            throw new RuntimeException("找不到该用户对应的角色");
        }
        // 查询已授权的收藏图层
        List<MapProjectDir> mapProjectDirList =mapLayerCollectMapper.listUserCollectLayers(roleIds, user.getUserId().toString(),projectId);
        // 图层目录
        mapProjectDirList.forEach(projectDir -> {
            projectDir.setIsCollect(1);
            if (projectDir.getIsLayerDir() == 1){
                // 图层目录
                List<Map<String, Object>> layerDirMap = this.listLayerDirMap(roleIds,projectDir.getId());
                projectDir.setLayerDirMap(layerDirMap);
            }
        });
        return mapProjectDirList;
    }


    /**
     * @描述: 获取图层目录下面的图层，需要用户拥有这个专题图层的权限
     * @入参 roleIds 用户角色id
     * @入参 projectDirId 专题目录id
     * @出参 List<Map<String, Object>> [{"2019": MapProjectDir},{"2020": MapProjectDir}]
     * @作者: 代鹏刚
     * @日期: 2022/4/28 19:05
     */
    @Override
    public List<Map<String, Object>> listLayerDirMap(List<String> roleIds, String projectDirId){
        // 找到图层目录的直接子集
        List<MapProjectDir> child = iMapProjectDirService.getSpecialMenu(roleIds, null, null, null, projectDirId);
        Map<String, Object> layerYear = child.stream().collect(Collectors.toMap(dir -> {
            // 图层目录下的图层，是以“xxxxx_2020”格式命名的，名称只要前面一截
            String alias = dir.getAlias();
            alias = StringUtils.hasText(alias) ? alias : dir.getName();
            String[] split = alias.split("_");
            return split == null ? "" : split.length >= 2 ? split[1] : split[0];
        }, dir -> {
            return dir;
        }));
        List<Map<String, Object>> layerDirMap = new LinkedList<>();
        layerYear.forEach(( key , value )->{
            HashMap<String, Object> stringObjectHashMap = new HashMap<>(1);
            stringObjectHashMap.put(key,value);
            layerDirMap.add(stringObjectHashMap);
        });
        return layerDirMap;
    }

}
